// pages/editstudent/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '', 
    phone: '',   
    current_school: '',
    sex: 0,
    certificate_no: '',
    items: [
      { name: '男', value: '0', checked: 'true' },
      { name: '女', value: '1' }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id
    })
    //添加学员
    wx.request({
      url: 'https://sng.tsletu.com/api/wxapp/user/getStudentsInfo',
      data: {
        'id': options.id
      },
      header: {
        'XX-Device-Type': 'wxapp',
        'XX-Token': app.globalData.token
      },
      success: res => {
        this.setData({
          name: res.data.data.name,
          phone: res.data.data.phone,
          current_school: res.data.data.current_school,
          sex: res.data.data.sex,
          certificate_no: res.data.data.certificate_no
        })
      }
    })
  },
  formSubmit: function (e) {
    var bool = true;
    if (!e.detail.value.school) {
      bool = false;
      wx.showToast({
        title: '在读学校名称不能为空',
        icon: 'none',
      })
    }
    if (!e.detail.value.phone) {
      bool = false;
      wx.showToast({
        icon: 'none',
        title: '联系电话不能为空',
        duration: 2000
      })
    } else if (e.detail.value.phone.length < 11) {
      bool = false;
      wx.showToast({
        icon: 'none',
        title: '联系电话格式不正确',
        duration: 2000
      })
    }  
    if(!e.detail.value.name){
      bool = false;
      wx.showToast({
        icon: 'none',
        title: '学员姓名不能为空',
      })
    }    
    if(bool){
      wx.showModal({
        title: '提示',
        content: '确定保存学员信息吗？',
        success: res => {
          if (res.confirm) {
            //修改学员
            wx.request({
              url: 'https://sng.tsletu.com/api/wxapp/user/editUserStudents',
              data: {
                'id': this.data.id,
                'name': e.detail.value.name,
                'phone': e.detail.value.phone,
                'current_school': e.detail.value.school,
                'certificate_no': e.detail.value.certificate,
                'sex': e.detail.value.sex
              },
              header: {
                'XX-Device-Type': 'wxapp',
                'XX-Token': app.globalData.token
              },
              success: res => {                
                if(res.data.code == 1){
                  wx.showToast({
                    icon: 'none',
                    title: '保存成功!',
                    duration: 2000
                  })                  
                  wx.navigateBack({
                    delta: 1
                  })
                }else{
                  wx.showToast({
                    icon: 'none',
                    title: '保存失败！',
                    duration: 2000
                  })
                }
              }
            })
          }
        }
      }) 
    }       
  },
  onDelStudent: function(res){
    wx.showModal({
      title: '提示',
      content: '确定要删除学员吗？',
      success: res => {
        //添加学员
        wx.request({
          url: 'https://sng.tsletu.com/api/wxapp/user/delUserStudents',
          data: {
            'id': this.data.id
          },
          header: {
            'XX-Device-Type': 'wxapp',
            'XX-Token': app.globalData.token
          },
          success: res => {
            if (res.data.code == 1) {
              wx.showToast({
                icon: 'none',
                title: '删除成功!',
                duration: 2000
              })
              wx.navigateBack({
                delta: 1,
              })
            } else {
              wx.showToast({
                icon: 'none',
                title: '删除失败！',
                duration: 2000
              })
            }
          }
        })
      }
    })    
  }  
})